package com.springcloud.zuul.filter;

import com.alibaba.fastjson.JSONObject;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import com.springcloud.zuul.common.constant.Constant;
import com.springcloud.zuul.common.utlis.ExceptionLogUtil;
import com.springcloud.zuul.common.utlis.HttpContextUtils;
import com.springcloud.zuul.entity.SysUserEntity;
import com.springcloud.zuul.entity.SysUserVo;
import com.springcloud.zuul.service.SysConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.codec.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;

@Component
@Slf4j
public class AdminZullFilter extends ZuulFilter {

    @Autowired
    SysConfigService sysConfigService;

    /**
     * Description:可以在请求被路由之前调用
     * @author zhlu
     * @date 2020/1/6 2:19 下午
     * @param
     * @return java.lang.String
     */
    @Override
    public String filterType() {
        return "pre";
    }

    /**
     * Description:filter执行顺序，通过数字指定 ,优先级为0，数字越大，优先级越低
     * @author zhlu
     * @date 2020/1/6 2:19 下午
     * @param
     * @return int
     */
    @Override
    public int filterOrder() {
        return 0;
    }

    /**
     * Description:表示是否需要执行该filter，true表示执行，false表示不执行
     * 是否执行该过滤器，此处为true，说明需要过滤
     * @author zhlu
     * @date 2019/12/18 7:04 下午
     * @param
     * @return boolean
     */
    @Override
    public boolean shouldFilter() {
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        String path = request.getServletPath();
        if (path.contains(sysConfigService.getValue("DO_BLOCK"))) {
            return true;
        }
        return false;
    }

    /**
     * 这里判断URL权限
     *
     * @return
     * @throws ZuulException
     */
    @Override
    public Object run() throws ZuulException {
        //判断权限
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();

        RequestContext ctx = RequestContext.getCurrentContext();

        SysUserEntity user = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
        //构建Vo 对象
        SysUserVo userVo = new SysUserVo();
        userVo.setUserId(user.getUserId());
        userVo.setUserName(user.getUserName());
        userVo.setEmail(user.getEmail());
        userVo.setPhone(user.getPhone());

        try {
            ctx.addZuulRequestHeader(Constant.HEAD_USER_ID, Base64.encodeToString(JSONObject.toJSONString(userVo).getBytes("UTF-8")));
            ctx.addZuulRequestHeader(Constant.HEAD_TYPE, String.valueOf(Constant.HeadType.SYS.getCode()));
        } catch (UnsupportedEncodingException e) {
            log.error(ExceptionLogUtil.getStackTrace(e));
        }

       log.info("转发服务URL ===> {}\n转发附带Header ====> {} \n 参数 ====> {}",
                request.getServletPath(),
                ctx.getZuulRequestHeaders().toString(),
                request.getParameterMap().toString());

        log.debug("转发服务URL ===> {}", request.getServletPath());
        return null;
    }

}
